package com.stbu;

import com.stbu.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;



@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/toReg")
    public String toReg(){
        return "reg";
    }

    @RequestMapping("/reg")
    public String reg(@RequestParam("username")String username,
                      @RequestParam("pwd")String password, Model model){
        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        Date regDate=new Date();
        model.addAttribute("regDate",regDate);
        model.addAttribute("user",user);
        return "forward:tologin";
    }
    @RequestMapping("/login")
    public String toLogin(){
        return "login";
    }
    public ModelAndView login(@RequestParam("username") String username,@RequestParam("pwd") String pwd,
                              @SessionAttribute("user") User user,
                              ModelAndView mView){

        String msg="";
        if (username.equalsIgnoreCase(user.getUsername())&&pwd.equalsIgnoreCase(user.getPassword())){
            msg="登录成功";
        }else {
            msg="登录失败";
        }
        mView.addObject("msg",msg);
        mView.addObject("username",username);
        mView.setViewName("result");
        return mView;


    }
}
